package com.xinyun.traceability.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinyun.traceability.enity.dto.CommodityDTO;
import com.xinyun.traceability.enity.po.Commodity;
import com.xinyun.traceability.enity.po.SysUser;
import com.xinyun.traceability.mapper.CommodityMapper;
import com.xinyun.traceability.service.CommodityService;
import com.xinyun.traceability.utils.CommonUtil;
import com.xinyun.traceability.utils.DateUtil;
import com.xinyun.traceability.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.Date;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Value("${qrcode.url}")
    private String qrcodeUrl;

    @Value("${commodity.picpath}")
    private String commodityPicpath;

    @Autowired
    CommodityMapper commodityMapper;

    @Override
    public PageInfo<Commodity> queryCommodityPage(CommodityDTO commodityDTO) {
        PageHelper.startPage(commodityDTO.getPage() + 1 , commodityDTO.getLimit());
        List<Commodity> commodityList = commodityMapper.selectByCommodityDTO(commodityDTO);
        return new PageInfo<>(commodityList);
    }

    @Override
    public void saveCommodity(Commodity commodity) {
        //1.新增
        if (commodity.getId() == null) {
            commodity.setCreateTime(new Date());
            commodity.setUpdateTime(new Date());
            SysUser currentUser = CommonUtil.getCurrentUser();
            commodity.setBusinessId(currentUser.getId());
            commodity.setScanCount(0);
            Resource resource = new DefaultResourceLoader().getResource("classpath:static/assets/img/header.jpg");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try {
                QRCodeUtils.encode(String.format(qrcodeUrl, commodity.getUuid()), resource.getFile().getAbsolutePath(), outputStream, true);
                String qrcode = Base64.getEncoder().encodeToString(outputStream.toByteArray());
                commodity.setQrcode(qrcode);
                commodityMapper.insertSelective(commodity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            //2.修改
            commodity.setUpdateTime(new Date());
            commodityMapper.updateByPrimaryKeySelective(commodity);
        }
    }

    @Override
    public Commodity selectCommodityByUuid(String uuid) {
        return commodityMapper.selectCommodityByUuid(uuid);
    }

    @Override
    public void updateByPrimaryKeySelective(Commodity commodity) {
        commodityMapper.updateByPrimaryKeySelective(commodity);
    }

    @Override
    public List<Commodity> selectCommodityByCommodityDTO(CommodityDTO commodityDTO) {
        return commodityMapper.selectByCommodityDTO(commodityDTO);
    }

    @Override
    public Commodity selectCommodityByPrimaryKey(Integer id) {
        return commodityMapper.selectByPrimaryKey(id);
    }
}
